<?php

namespace App\Models\Common;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class Media extends Model
{
	use HasDateTimeFormatter;
    use SoftDeletes;

    protected $table = 'medias';

    protected $guarded = [];

    protected $appends = ['path'];

    public function file() : BelongsTo
    {
        return $this->belongsTo(File::class, 'file_id');
    }

    public function ext(): Attribute
    {
        return Attribute::make(
            get: fn () => $this->file->ext ?? '',
        );
    }

    public function path(): Attribute
    {
        return Attribute::make(
            get: fn () => storage_path('app/' . $this->file->url),
        );
    }
}
